/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.dao.hibernate;

import com.mavenrentcars.dao.RatingDao;
import com.mavenrentcars.model.Car;
import com.mavenrentcars.model.Rating;
import com.mavenrentcars.model.User;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 *
 * @author student
 */
@Repository("ratingDao")
public class RatingDaoHibernate extends GenericDaoHibernate<Rating, Integer> implements RatingDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    public RatingDaoHibernate(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
        super(Rating.class);
        setSessionFactory(sessionFactory);
    }

    @Override
    public Rating get(Car car, User user) {
        List<Rating> res = this.getHibernateTemplate().find(
                "from " + Rating.class.getName()
                + " obj where obj.car = ? and obj.user = ?",
                car, user);
        if (res.isEmpty()) {
            return null;
        }

        return res.get(0);
    }
}
